package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayout.Tab;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.android.material.tabs.TabLayoutMediator.TabConfigurationStrategy;
import easier.recycler.DataViewHolder;
import easier.recycler.SingleTypeAdapter;
import java.util.ArrayList;
import java.util.List;

public class MainActivity117 extends AppCompatActivity {

    private CoordinatorLayout coordinator;
    private AppBarLayout appbar;
    private CollapsingToolbarLayout collapse;
    private ImageView image;
    private TabLayout tabLayout;
    private ViewPager2 viewPager;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity117.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main117 );
        initView();
    }

    private void initView() {

        coordinator = (CoordinatorLayout) findViewById( R.id.coordinator );
        appbar = (AppBarLayout) findViewById( R.id.appbar );
        collapse = (CollapsingToolbarLayout) findViewById( R.id.collapse );
        image = (ImageView) findViewById( R.id.image );
        tabLayout = (TabLayout) findViewById( R.id.tabLayout );
        viewPager = (ViewPager2) findViewById( R.id.viewPager2 );

        viewPager.setAdapter( new FragmentStateAdapter( this ) {
            @NonNull
            @Override
            public Fragment createFragment( int position ) {

                return new Item0Fragment();
            }

            @Override
            public int getItemCount() {

                return 4;
            }
        } );

        new TabLayoutMediator( tabLayout, viewPager, new TabConfigurationStrategy() {
            @Override
            public void onConfigureTab( @NonNull Tab tab, int position ) {

                tab.setText( "tab " + position );
            }
        } ).attach();
    }

    public static class Item0Fragment extends Fragment {

        @Nullable
        @Override
        public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState ) {

            return new RecyclerView( inflater.getContext() );
        }

        @Override
        public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {

            super.onViewCreated( view, savedInstanceState );

            RecyclerView recyclerView = (RecyclerView) view;
            recyclerView.setLayoutManager( new LinearLayoutManager( view.getContext() ) );

            SingleTypeAdapter< String > adapter = new SingleTypeAdapter< String >() {
                @Override
                protected DataViewHolder< String > buildHolder( int position, String o ) {

                    return new DataViewHolder<>( R.layout.activity_main_99_item, o ) {
                        @Override
                        protected void onBind( ViewHolder viewHolder, View root, int position, int type,
                            List< Object > payloads ) {

                            ((TextView) root.findViewById( R.id.text )).setText( getElement() );
                        }
                    };
                }
            };
            List< String > data = new ArrayList<>();
            for ( int i = 0; i < 100; i++ ) {
                data.add( "item " + String.valueOf( i ) );
            }
            adapter.updateSource( data );
            recyclerView.setAdapter( adapter );
        }
    }
}